package com.bike.bikeserver.tasks;

import com.alibaba.fastjson2.JSONObject;
import com.bike.bikeserver.common.enums.ModelParamsEnums;
import com.bike.bikeserver.common.enums.ZoneEnums;
import com.bike.bikeserver.common.utils.ModelUtils;
import com.bike.bikeserver.domain.Zone;
import com.bike.bikeserver.domain.ZoneBikeSummary;
import com.bike.bikeserver.mapper.ZoneBikeSummaryMapper;
import com.bike.bikeserver.mapper.ZoneMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.bike.bikeserver.constant.ModelParamConstant.YOLO_NO_OV_URL;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/8/21 20:08
 */
@Component
@Slf4j
public class ScheduledBikeSummaryTask
{
    @Autowired
    ZoneMapper zoneMapper;
    @Autowired
    ZoneBikeSummaryMapper zoneBikeSummaryMapper;

    /**
     * 定时维持Zone库到指定个区域
     */
    @Scheduled(cron = "0 0 12 */30 * *") // 每30天
    public void enrichZones()
    {
        log.info("定时维持Zone库到指定个区域 at: " + new Date());
        // 检索所有zone并判断是否小于设置好的合理区域数量
        List<Zone> zones = zoneMapper.selectList(null);
        // 如果小于设置好的合理区域数量，就把所有记录删除，重新插入
        if (zones.size() == 0)
        {
            // 插入记录（目前为8个）
            List<Zone> zoneList = new ArrayList<>();
            zoneList.add(new Zone(ZoneEnums.SSPU.getDisplayName(), ZoneEnums.SSPU.getValue()));
            zoneList.add(new Zone(ZoneEnums.GUTANG_ROAD.getDisplayName(), ZoneEnums.GUTANG_ROAD.getValue()));
            zoneList.add(new Zone(ZoneEnums.JINHAI_ROAD.getDisplayName(), ZoneEnums.JINHAI_ROAD.getValue()));
            zoneList.add(new Zone(ZoneEnums.MIDDLEYANGGAO_ROAD.getDisplayName(), ZoneEnums.MIDDLEYANGGAO_ROAD.getValue()));
            zoneList.add(new Zone(ZoneEnums.JINJI_ROAD.getDisplayName(), ZoneEnums.JINJI_ROAD.getValue()));
            zoneList.add(new Zone(ZoneEnums.GUOQUAN_ROAD.getDisplayName(), ZoneEnums.GUOQUAN_ROAD.getValue()));
            zoneList.add(new Zone(ZoneEnums.JIANGPU_PARK.getDisplayName(), ZoneEnums.JIANGPU_PARK.getValue()));
            zoneList.add(new Zone(ZoneEnums.CAO_ROAD.getDisplayName(), ZoneEnums.CAO_ROAD.getValue()));
            // 批量插入
            for (Zone zone : zoneList)
            {
                zoneMapper.insert(zone);
            }
        }
    }

    /**
     * 定时更新各区域单车数目分布
     */
    @Scheduled(cron = "0 0 0 */13 * *") // 每13天
    public void enrichBikes()
    {
        // 检索所有zoneBikeSummary并判断是否小于设置好的合理数量
        List<ZoneBikeSummary> zoneBikeSummaries = zoneBikeSummaryMapper.selectList(null);

        if (zoneBikeSummaries.size() < Integer.parseInt(ZoneEnums.ZONE_NUMS.getValue()))
        {
            List<Zone> zoneList = zoneMapper.selectList(null);
            for (Zone zone : zoneList)
            {
                List<String> imgCodes = new ArrayList<>();
                imgCodes.add(zone.getCoordinateCode());
                JSONObject params = new JSONObject();
                params.put("imageCodes", imgCodes);
                String yoloResult = ModelUtils.getYoloResult(YOLO_NO_OV_URL, params);
                Integer shared_bike_num = ModelUtils.getObjNumByPattern(yoloResult, ModelParamsEnums.SHARED_BIKE_PATTERN.getValue());
                Integer private_bike_num = ModelUtils.getObjNumByPattern(yoloResult, ModelParamsEnums.PRIVATE_BIKE_PATTERN.getValue());
                Integer motor_num = ModelUtils.getObjNumByPattern(yoloResult, ModelParamsEnums.MOTOR_PATTERN.getValue());
                Integer sum_num = shared_bike_num + private_bike_num + motor_num;
                ZoneBikeSummary zoneBikeSummary = new ZoneBikeSummary(zone.getId(), shared_bike_num, private_bike_num, motor_num, sum_num);
                zoneBikeSummaryMapper.insert(zoneBikeSummary);
            }
        }
    }
}
